package edu.washington.vaccine.ui;

import android.app.TabActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import edu.washington.vaccine.R;
import edu.washington.vaccine.backend.VaccineDB;
import edu.washington.vaccine.backend.VaccineLocalHelper;

/*
 * Operates three tabs
 * Patient tab: add/find patients
 * Vaccine tab: view Vaccine info
 * Sunc tab: Sync local data with online data
 * 
 * Application Path Layout:
 * 
 * 									Login
 * 									  |
 * 								MainActivity
 * 				|					  |								|
 *		   Vaccine Tab			  Patient Tab			          Synch Tab
 *		VaccineGroupActivity	 VaccineActivity		        CachActivity
 *			|			         |	           |			
 *VaccineListActivity   SearchPatientActivity  AddPatientActivity
 *			|			         |	           |
 *VaccineDataActivity	SearchResultsActivity  |
 *							     |	           |
 *							 PatientDataActivity
 *							  |            |
 *			  ChooseVaccineActivity  EditPatientActivity
 *							  |	
 *					PostChooseVaccineActivity
 * 
 * 
 * 
 * Local and Online SQL Tables:
 * Vaccine Tables	Patient Data	PatientHistory
 * mimics			mimics			uses
 * Vaccine Class	PatientClass	p.id, v.id, dategiven
 * all tabs remain persistent throughout the entire use of the application
 */
public class MainActivity extends TabActivity {
	@SuppressWarnings("unused")
	private static final String TAG = "MainActivity";
	VaccineDB db = VaccineDB.getInstance();
		
//	private Context context;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initializeTabs();
		initializeTables();
	}
	
    public boolean onCreateOptionsMenu(Menu menu) {
//    	super.onCreateOptionsMenu(menu);
		menu.add("Online/Offline Mode");
		return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	if (db.isOnlineMode()) {
    		Toast.makeText(getApplicationContext(), "You are now working in offline mode", Toast.LENGTH_LONG).show();
    		CacheActivity.downloadPatients(VaccineLocalHelper.getInstance(this).getWritableDatabase());
    		CacheActivity.downloadPatientHistory(VaccineLocalHelper.getInstance(this).getWritableDatabase());
    		db.setOnlineMode(false);
    	} else {
    		Toast.makeText(getApplicationContext(), "You are now working in online mode", Toast.LENGTH_LONG).show();
    		db.setOnlineMode(true);
    		VaccineLocalHelper.getInstance(this).update();
    	}
    	return true;
    }
	
	private void initializeTabs() {
		createTab(R.drawable.tab, "Patients", FirstGroup.class);
		createTab(R.drawable.tab, "Vaccines", VaccineGroupActivity.class);
		createTab(R.drawable.tab, "Sync", CacheActivity.class);
	}
	
	private void initializeTables() {
		SQLiteDatabase db = VaccineLocalHelper.getInstance(this).getWritableDatabase();
		db.execSQL("create table if not exists Patients (pid text, lname text, fname text, dob text, location text, fathername text, mothername text, editdate text, conflict text, notes text, primary key(pid, editdate))");
		db.execSQL("create table if not exists PatientHistory (pid text, vid text, dategiven text, primary key (pid, vid, dategiven))");

		db.execSQL("create table if not exists newPatients (pid text, lname text, fname text, dob text, location text, fathername text, mothername text, editdate text, conflict text, notes text, primary key(pid, editdate))");
		db.execSQL("create table if not exists newPatientHistory (pid text, vid text, dategiven text, primary key (pid, vid, dategiven))");

		db.execSQL("create table if not exists editedPatients (pid text, lname text, fname text, dob text, location text, fathername text, mothername text, editdate text, conflict text, notes text, primary key(pid, editdate))");
		db.execSQL("create table if not exists editedPatientHistory (pid text, vid text, dategiven text, primary key (pid, vid, dategiven))");
	}
	
	private void createTab(int drawableId, String labelId, Class<?> activity) {
		TabHost tabHost = getTabHost();
		Intent intent = new Intent(this, activity);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);	
		
		View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tindicator, getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);
		//tabIndicator.setBackgroundColor(R.color.white);
		//ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		//icon.setImageResource(drawableId);
		
		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}
}